//
//  HomeTabBarController.m
//  LianMengPOC
//
//  Created by WangQi on 13-6-20.
//
//

#import "HomeTabBarController.h"
#import "SearchViewController.h"
#import "NoteViewController.h"
#import "SettingViewController.h"

@interface HomeTabBarController ()

@end

@implementation HomeTabBarController

static HomeTabBarController * homeTabBarController = nil;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)init
{
    if(self = [super init]){
        homeTabBarController = self;
        self.delegate = self;
        
        SearchViewController *searchViewController = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil];
        NoteViewController *noteViewController = [[NoteViewController alloc] initWithNibName:@"NoteViewController" bundle:nil];
        SettingViewController *settingViewController = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];
        
        UINavigationController *searchNavController = [[UINavigationController alloc] initWithRootViewController:searchViewController];
        UINavigationController *noteNavController = [[UINavigationController alloc] initWithRootViewController:noteViewController];
        UINavigationController *settingNavController = [[UINavigationController alloc] initWithRootViewController:settingViewController];
        
        [searchNavController setNavigationBarHidden:YES];
        [noteNavController setNavigationBarHidden:YES];
        [settingNavController setNavigationBarHidden:YES];
        
        [self setViewControllers:[NSArray arrayWithObjects:searchNavController,noteNavController,settingNavController, nil]];
        
        [searchViewController release];
        [noteViewController release];
        [settingViewController release];
        
        [searchNavController release];
        [noteNavController release];
        [settingNavController release];
        
        self.tabBar.hidden = YES;
        [self setContentViewHeight];
        
    }
    return self;
}

+ (HomeTabBarController *)sharedHomeTabBarController
{
    return homeTabBarController;
}

- (void)setContentViewHeight
{
    UIView * contentView;
    if([[self.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]]){
        contentView = [self.view.subviews objectAtIndex:1];
    }
    else{
        contentView = [self.view.subviews objectAtIndex:0];
    }
    contentView.height = SCREEN_HEIGHT;
}

- (void)selectTabBarAtIndex:(int)index
{
    if(self.selectedIndex!=index){
        self.selectedIndex=index;
    }
}

- (void)hideTabBarAnimated:(BOOL)animated
{
    float animationTime = animated?0.3:0;
    [self.view bringSubviewToFront:self.homeTabBar];
    [UIView animateWithDuration:animationTime animations:^{
        self.homeTabBar.top = self.view.height+2;
    }];
}

- (void)showTabBarAnimated:(BOOL)animated
{
    float animationTime = animated?0.3:0;
    [self.view bringSubviewToFront:self.homeTabBar];
    [UIView animateWithDuration:animationTime animations:^{
        self.homeTabBar.bottom = self.view.height;
    }];
   
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	self.homeTabBar = [HomeTabBar viewFromNib];
    self.homeTabBar.bottom = SCREEN_HEIGHT;
    self.homeTabBar.delegate = self;
    [self.view addSubview:self.homeTabBar];
}

- (void)homeTabBar:(HomeTabBar *)homeTabBar didSelectTabAtIndex:(int)index
{
    [self selectTabBarAtIndex:index];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [_homeTabBar release];
    [super dealloc];
}

@end






